package com.interfaces.module.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.interfaces.module.common.controller.CommonController;
import com.interfaces.module.goods.domain.FtsGoods;
import com.interfaces.module.goods.service.FtsGoodsService;
import com.interfaces.util.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;

@RestController
@RequestMapping("/goods")
public class FtsGoodsController extends CommonController<FtsGoods> {
    @Autowired
    private FtsGoodsService ftsGoodsService;
    @Override
    protected IService<FtsGoods> getCoreService() {
        return this.ftsGoodsService;
    }

    public Result getDataById(@PathVariable Serializable id, String where){
        FtsGoods t;
        QueryWrapper<FtsGoods> queryWrapper=new QueryWrapper<FtsGoods>();
        if(StringUtils.isNotEmpty(where)){
            if(where.contains("&")){
                String[] whereDatas=where.split("&");
                for (String whereStr:whereDatas) {
                    String[] whereParam=whereStr.split("=");
                    queryWrapper.eq(whereParam[0],whereParam[1]);
                }
            }else{
                String[] whereParam=where.split("=");
                queryWrapper.eq(whereParam[0],whereParam[1]);
            }

            t=getCoreService().getOne(queryWrapper);
        }else{
            t=getCoreService().getById(id);
        }
        String productImage=t.getProductImage();
        if(null!=productImage&&productImage.length()>0){
            t.setProductImageList(productImage.split(","));
        }
        return Result.SUCCESS(t);
    }
}
